package com.huaxindata.im.model.contact;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.huaxin.common.view.avatarimageview.AvatarImageView;
import com.huaxin.common.view.avatarimageview.HxAvatarImageGlide;
import com.huaxindata.im.R;
import com.huaxindata.im.model.utils.HanziToPinyin;

/**
 * Created by Administrator on 2017/8/16.
 * 所有联系人的父类，默认的所有类都含有此六种属性
 */

public class Contact implements Comparable<Contact> , Parcelable{
    public String clientId;//客户唯一id
    public String icon;//头像
    public String nickName;//昵称
    public String duty;
    public String phone;//电话
    public String company;//所属公司组织

    private char first = ' ';

    private String mFirstName;

    public LatLng getLatLng() {
        return mLatLng;
    }

    public void setLatLng(LatLng latLng) {
        mLatLng = latLng;
    }

    private LatLng mLatLng;//百度地理位置坐标

    public Contact() {
    }

    public Contact(String clientId, String icon, String nickName) {
        this.clientId = clientId;
        this.icon = icon;
        this.nickName = nickName;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getDuty() {
        return duty;
    }

    public void setDuty(String duty) {
        this.duty = duty;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getFirstName() {
        return String.valueOf(getFirstChar());
    }

    //没有头像时，显示的头像名称
    public String getFirstNames() {
        if (mFirstName != null)
            return mFirstName;
        if (nickName == null)
            return null;
        if (nickName.length() <= 2)
            return mFirstName = nickName;
        return mFirstName = nickName.substring(nickName.length() - 2, nickName.length());
    }

    public char getFirstChar() {
        if (getNickName() == null) {
            return 'a';
        }
        if (first == ' ') {
            first = HanziToPinyin.getFirstChar(nickName);
        }
        return first;
    }

    public static final Parcelable.Creator<ContactPerson> CREATOR = new Parcelable.Creator<ContactPerson>() {
        @Override
        public ContactPerson createFromParcel(Parcel in) {
            return new ContactPerson(in);
        }

        @Override
        public ContactPerson[] newArray(int size) {
            return new ContactPerson[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(clientId);
        dest.writeString(icon);
        dest.writeString(nickName);
    }

    @Override
    public int compareTo(Contact another) {
        //如果是组织单位，则默认在前排
        final char thisChar = this instanceof ContactGroup ? Character.toUpperCase(getFirstChar()
        ) : Character.toLowerCase(getFirstChar());
        final char anoChar = another instanceof ContactGroup ? Character.toUpperCase(another
                .getFirstChar()) : Character.toLowerCase(another.getFirstChar());
        if (anoChar > thisChar) {
            return -1;
        } else if (anoChar == thisChar) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public String toString() {
        return "Contact{" +
                "clientId='" + clientId + '\'' +
                ", icon='" + icon + '\'' +
                ", nickName='" + nickName + '\'' +
                ", duty='" + duty + '\'' +
                ", phone='" + phone + '\'' +
                ", company='" + company + '\'' +
                ", first=" + first +
                ", mFirstName='" + mFirstName + '\'' +
                ", mLatLng=" + mLatLng +
                '}';
    }

    /**
     * 设置头像，没有头像url则设置为昵称前两个字
     * 作为头像
     *
     * @param context ：上下文
     * @param iv      ：
     * @param tv      ：如果没有头像url则显示文字
     */
    public void showImageIcon(Context context, AvatarImageView iv, TextView tv) {


        if (this instanceof ContactGroup) {
            if (!TextUtils.isEmpty(getIcon())) {
                iv.setVisibility(View.VISIBLE);
                Glide.with(context).load(getIcon()).apply(new RequestOptions()
                        .placeholder(R.mipmap.new_group)).into(iv);
                if (tv != null)
                    tv.setVisibility(View.GONE);
            } else {
                if (tv != null)
                    tv.setVisibility(View.GONE);
                iv.setImageResource(R.mipmap.new_group);
            }
        } else {

            //iv.setVisibility(View.GONE);
            HxAvatarImageGlide instance = HxAvatarImageGlide.getInstance();
            instance.setHeadImage(context, getIcon(), getFirstNames(), iv);

           /* if (!TextUtils.isEmpty(getIcon())) {
                iv.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(getIcon())
                        .into(iv);
                if (tv != null)
                    tv.setVisibility(View.GONE);
            } else {
                iv.setVisibility(View.GONE);
                HxAvatarImageGlide instance = HxAvatarImageGlide.getInstance();
                instance.setHeadImage(context, getIcon(), getFirstNames(), iv);
*/

              /*  if (tv != null) {


                    instance.setHeadImage(context, "", getFirstNames(), iv);


                   *//* tv.setVisibility(View.VISIBLE);
                    tv.setBackgroundResource(RandomIconDrawable.getNameDrawable(getClientId()));
                    tv.setText(getFirstNames());*//*
                } else {
                    iv.setVisibility(View.VISIBLE);
                    iv.setImageResource(R.mipmap.new_people);
                }
        }*/
        }
    }
}
